<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10
 * Time: 10:57
 */

namespace app\api\controller;
require_once BASE_ROOT."core/application/api/controller/BaseController.php";
require_once BASE_ROOT."core/application/api/service/UserwapService.php";
use think\Request;
use think\Log;
use app\api\service\UserwapService;
use app\api\service\UserService;
class Userwap extends BaseController
{
    /**
     * 获取用户id
     * @return string
     */
    public function Login()
    {
        $rs = array('code' => 0,'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $user_name  = Request::instance()->param('user_name');
        $password  = Request::instance()->param('password');
        $rule = [
            ['user_name','require'],
            ['password','require'],
            ['mch_id','require','不存在商户'],
        ];
        $data = [
            'user_name'  => $user_name,
            'password'  => $password,
            'mch_id'  => $mch_id
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $user = new UserwapService();
        $info = $user->checkUsername($user_name,$mch_id);
        if(empty($info))
        {
            $rs['code'] = 1;
            $rs['msg']='用户名不存在';
            return json_encode($rs);
        }
        $userInfo=$user->checkUser($user_name,$password,$mch_id);
        if(empty($userInfo)){
            $rs['code'] = 1;
            $rs['msg']='密码错误';
            return json_encode($rs);
        }
        $rs['msg'] = "登录成功";
        $rs['info'] = $userInfo;
        return json_encode($rs);
    }
    public function register(){
        $rs = array('code' => 0,'info' => array());
        $app_id = Request::instance()->param('i');       //小程序id
        $mch_id = $this->getMchId($app_id);
        $nick_name  = Request::instance()->param('nick_name');
        $user_name  = Request::instance()->param('user_name');
        $password  = Request::instance()->param('password');
        $rule = [
            ['nick_name','require'],
            ['user_name','require'],
            ['password','require'],
            ['mch_id','require','不存在商户'],
        ];
        $data = [
            'nick_name'  => $nick_name,
            'user_name'  => $user_name,
            'password'  => $password,
            'mch_id'  => $mch_id
        ];
        $result = $this->checkParam($rule,$data);
        if(!empty($result)){
            $rs['code'] = 1;
            $rs['msg'] = $result;
            return json_encode($rs);
        }
        $userwap = new UserwapService();
        $info = $userwap->checkUsername($user_name,$mch_id);
        if(!empty($info))
        {
            $rs['code'] = 1;
            $rs['msg']='用户名已存在';
            return json_encode($rs);
        }
        $data = [
            'user_headimg'  => '',
            'nick_name'  => $nick_name,
            'wx_openid'=>time(),
            'reg_time'  => time(),
            'mch_id'  => $mch_id,
            'is_distributor'=>0,
            'pid'=>0,
            'total_price'=>0,
            'price'=>0,
            'wx_unionid'=>'',
            'user_type'=>3
        ];
        $uid = $userwap->addUser($data);
        if(empty($uid))
        {
            $rs['code'] = 1;
            $rs['msg'] = "用户注册失败";
            return json_encode($rs);
        }
        $datawap = [
            'uid'  => $uid,
            'mch_id'  => $mch_id,
            'user_name'=>$user_name,
            'phone'=>"",
            'password'=>md5($password),
            'create_time'=>time()
        ];
        $result=$userwap->addUserwap($datawap);
        if(empty($result))
        {
            $rs['code'] = 1;
            $rs['msg'] = "用户注册失败";
            return json_encode($rs);
        }
        $data['uid']=$uid;
        $rs['info'] = $data;
        return json_encode($rs);
    }
}